﻿/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Xml.Serialization;

namespace LucasCode.PublicTypes
{
    [Serializable]
    public class MatchContestant
    {
        #region Private Properties
        private readonly List<MatchPlayer> m_Players = new List<MatchPlayer>();
        private Bitmap m_Logo;
        private MatchContestantTypes m_Type = MatchContestantTypes.Team;

        #endregion

        #region Properties
        public string Name { get; set; }

        public string ID { get; set; }

        public bool IsOwnerTeam { get; set; }

        public MatchContestantTypes Type
        {
            get { return m_Type; }
            set { m_Type = value; }
        }

        public MatchPlayer[] Players
        {
            get { return m_Players.ToArray(); }
            set
            {
                m_Players.Clear();
                foreach (MatchPlayer p in value)
                    addPlayer(p);
            }
        }

        [XmlElementAttribute("Logo")]
        public byte[] PictureByteArray
        {
            get
            {
                if (m_Logo != null)
                {
                    TypeConverter BitmapConverter =
                         TypeDescriptor.GetConverter(m_Logo.GetType());
                    return (byte[])
                         BitmapConverter.ConvertTo(m_Logo, typeof(byte[]));
                }
                else
                    return null;
            }
            set
            {
                if (value != null)
                    m_Logo = new Bitmap(new System.IO.MemoryStream(value));
                else
                    m_Logo = null;
            }
        }

        [XmlIgnore]
        public int PlayerCount
        {
            get { return m_Players.Count; }
        }

        [XmlIgnore]
        public Bitmap Logo
        {
            get { return m_Logo; }
            set { m_Logo = value; }
        }

        [XmlIgnore]
        public string URL
        {
            get
            {
                if (string.IsNullOrEmpty(ID))
                    return string.Empty;
                switch (m_Type)
                {
                    case MatchContestantTypes.Individual:
                        return String.Concat("http://www.esl.eu/player/", ID, "/");
                    case MatchContestantTypes.Team:
                        return String.Concat("http://www.esl.eu/team/", ID, "/");
                    default:
                        return string.Empty;
                }
            }
        }
        #endregion

        #region Methods
        public void addPlayer(MatchPlayer player)
        {
            m_Players.Add(player);
            player.Team = this;
        }

        public MatchPlayer searchDuplicate(MatchPlayer player)
        {
            foreach (MatchPlayer p in m_Players)
            {
                if (p.Equals(player))
                {
                    return p;
                }
            }
            return null;
        }

        public bool containsPlayer(MatchPlayer player)
        {
            if (m_Players.Contains(player))
                return true;

            return false;
        }

        public void removePlayer(MatchPlayer player)
        {
            m_Players.Remove(player);
        }     
        #endregion
    }
}
